export default {
  props: {
    value: {
      type: Object
    }
  },
  data () {
    return {
      data: {},
      labelWidth: "75px"
    };
  },
  computed: {
    title () {
      return this.value?.title;
    },
    label () {
      return this.value?.label;
    },
    type () {
      return this.value?.type;
    },
    group () {
      return this.value?.group;
    },
    control () {
      return this.value?.control;
    },
    url () {
      return this.value?.url;
    },
    hidden () {
      return this.value?.hidden;
    },
    disabled () {
      return this.value?.disabled;
    },
    name () {
      return this.value?.name;
    },
    sortable () {
      return this.value?.sortable;
    },
    defaultValue () {
      return this.value?.value;
    },
    icon () {
      return this.value?.icon;
    },
    actions () {
      return this.value?.actions;
    },
    added () {
      return this.value?.added;
    },
    preset () {
      return this.value?.preset;
    },
    width () {
      return this.value?.width;
    },
  },
  created () {
    this.data = this.createData();
  },
  methods: {
    createData () {
      return { ...this.value };
    },
    handleInput (name, value) {
      this.data[name] = value;
      this.$emit("input", this.data);
    },

    handleDisabled (value) {
      this.handleInput("disabled", !value);
    },
    handleHidden (value) {
      this.handleInput("hidden", !value);
    },
    handleSortable (value) {
      this.handleInput("sortable", value);
    },
    handleTitle (value) {
      this.data.title = value;
      this.$emit("input", this.data);
    },
    handleLabel (value) {
      this.data.label = value;
      this.$emit("input", this.data);
    },
    handleTip (value) {
      this.data.label = value;
      this.$emit("input", this.data);
      var str = "99999999{aaa}?{bbb}?{ccc}";
      var r = str.match(/{.*?}/gi);
      for (var index = 0; index < r.length; index++) {
        str = str.replace(r[0], "111");
        str = str.replace(r[1], "222");
      }
    },
    handleName (value) {
      this.data.name = value;
      this.$emit("input", this.data);
    },
    handleDefaultValue (value) {
      this.data.defaultValue = value;
      this.$emit("input", this.data);
    },

    handleUrl (value) {
      this.data.url = value;
      this.$emit("input", this.data);
    },
    handleType (value) {
      this.data.type = value;
      this.$emit("input", this.data);
    },
    handleGroup (value) {
      this.data.group = value;
      this.$emit("input", this.data);
    },
    handleControl (value) {
      this.data.control = value;
      this.$emit("input", this.data);
    },
    handleIcon (value) {
      this.handleInput("icon", value);
    },
    handleActions (value) {
      this.handleInput("actions", value);
    },
    handleAdded (value) {
      this.handleInput("added", value);
    },
    handlePreset (value) {
      this.handleInput("preset", value);
    },
    handleColumn (value) {
      this.data.width = value;
      this.$emit("input", this.data);
    }
  },
  watch: {
    value () {
      this.data = this.createData();
    }
  }
};
